#!/usr/bin/env node

console.log('🔍 最终导入路径测试\n')

const fs = require('fs')
const path = require('path')

// 检查所有文件的导入路径
const filesToCheck = [
  {
    file: 'main.uts',
    location: '根目录',
    expectedImports: [
      './src/components/BaseButton.uvue',
      './src/components/BaseInput.uvue'
    ]
  },
  {
    file: 'App.uvue',
    location: '根目录',
    expectedImports: [
      './src/store'
    ]
  },
  {
    file: 'src/pages/index/index.uvue',
    location: 'src 目录内',
    expectedImports: [
      '@/api',
      '@/store',
      '@/types'
    ]
  },
  {
    file: 'src/pages/login/login.uvue',
    location: 'src 目录内',
    expectedImports: [
      '@/api',
      '@/store',
      '@/types'
    ]
  },
  {
    file: 'src/pages/register/register.uvue',
    location: 'src 目录内',
    expectedImports: [
      '@/api',
      '@/store',
      '@/types'
    ]
  },
  {
    file: 'src/api/index.ts',
    location: 'src 目录内',
    expectedImports: [
      '@/utils/request',
      '@/types'
    ]
  },
  {
    file: 'src/store/index.ts',
    location: 'src 目录内',
    expectedImports: [
      '@/utils/storage',
      '@/types'
    ]
  },
  {
    file: 'src/utils/request.ts',
    location: 'src 目录内',
    expectedImports: [
      './storage',
      '@/types'
    ]
  }
]

console.log('📁 检查文件导入路径:')

let allValid = true

filesToCheck.forEach(({ file, location, expectedImports }) => {
  const filePath = path.join(process.cwd(), file)

  if (!fs.existsSync(filePath)) {
    console.log(`  ❌ ${file} - 文件不存在`)
    allValid = false
    return
  }

  const content = fs.readFileSync(filePath, 'utf8')
  console.log(`  ✅ ${file} (${location})`)

  expectedImports.forEach(expectedImport => {
    if (content.includes(expectedImport)) {
      console.log(`    ✅ 找到导入: ${expectedImport}`)
    } else {
      console.log(`    ❌ 缺少导入: ${expectedImport}`)
      allValid = false
    }
  })

  // 检查是否有错误的导入路径
  const wrongImports = [
    '../src/',
    './src/',
    '@/components/',
    '@/api/',
    '@/store/',
    '@/types/',
    '@/utils/',
    '@/config/',
    '@/pages/'
  ]

  wrongImports.forEach(wrongImport => {
    if (content.includes(wrongImport) && !expectedImports.includes(wrongImport)) {
      console.log(`    ⚠️  发现可能的错误导入: ${wrongImport}`)
    }
  })
})

// 检查路径别名配置
console.log('\n⚙️  检查路径别名配置:')

// 检查 vite.config.ts
const viteConfigPath = path.join(process.cwd(), 'vite.config.ts')
if (fs.existsSync(viteConfigPath)) {
  const viteConfig = fs.readFileSync(viteConfigPath, 'utf8')
  if (viteConfig.includes('@/') && viteConfig.includes('resolve')) {
    console.log('  ✅ vite.config.ts - 路径别名配置正确')
  } else {
    console.log('  ❌ vite.config.ts - 路径别名配置缺失')
    allValid = false
  }
} else {
  console.log('  ❌ vite.config.ts - 文件不存在')
  allValid = false
}

// 检查 tsconfig.json
const tsconfigPath = path.join(process.cwd(), 'tsconfig.json')
if (fs.existsSync(tsconfigPath)) {
  const tsconfig = fs.readFileSync(tsconfigPath, 'utf8')
  if (tsconfig.includes('@/*') && tsconfig.includes('paths')) {
    console.log('  ✅ tsconfig.json - 路径映射配置正确')
  } else {
    console.log('  ❌ tsconfig.json - 路径映射配置缺失')
    allValid = false
  }
} else {
  console.log('  ❌ tsconfig.json - 文件不存在')
  allValid = false
}

// 最终结果
console.log('\n🎯 测试结果:')
if (allValid) {
  console.log('  ✅ 所有导入路径配置正确！')
  console.log('\n📝 导入路径策略:')
  console.log('  - 根目录文件: 使用相对路径 (./src/)')
  console.log('  - src 目录内文件: 使用别名路径 (@/)')
  console.log('  - 同目录文件: 使用相对路径 (./)')

  console.log('\n🚀 框架现在可以正常运行！')
} else {
  console.log('  ❌ 导入路径配置存在问题，请检查上述错误')
}

console.log('\n🎉 导入路径测试完成！')
